from aip import AipSpeech
from API_KEY import *
from audio_record_play import *

def create_client():
    """ 你的 APPID AK SK """
    APP_ID = bd_appid
    API_KEY = bd_apikey
    SECRET_KEY = bd_api_secret

    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    return client


# 读取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


def speech_recognition(client, file_name):
    # 识别本地文件
    res = client.asr(get_file_content(file_name), 'wav', 16000, {
        'dev_pid': 1537,
    })
    return res["result"][0]

def tts_baidu(text):
    result = client.synthesis(text, 'zh', 1, {
        'vol': 5,
        'per': 4144
    })
    # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
    if not isinstance(result, dict):
        with open('data/audio_baidu.mp3', 'wb') as f:
            f.write(result)


if __name__ == '__main__':
    client = create_client()
    # 语音识别
    # result = speech_recognition(client, "data/output.wav")
    # print(result)
    # 语音合成
    tts_baidu("轩辕黄帝是中华民族的文明始祖。三月三,拜轩辕,是中华民族的传统,肇始于春秋,绵延至今。")
    play_audio('data/audio_baidu.mp3')

